import os

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
from django.dispatch import receiver


class Course(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    addTime = models.DateTimeField(auto_now_add=True)
    startTime = models.DateTimeField()
    endTime = models.DateTimeField()
    manageTeam = models.ForeignKey('user.Team', on_delete=models.CASCADE, related_name="manage")
    teams = models.ManyToManyField('user.Team', related_name="join")
    info = models.TextField(max_length=500, blank=True)
    image = models.ImageField(upload_to='course/img', default='course/default.png')


class Video(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    typeChoices = [(0, 'B站视频BV号'), (1, '指定播放页面URL(视频)'), (3, '指定播放页面URL(直播)'), (2, '上传到服务器')]
    uploadTime = models.DateTimeField(auto_now_add=True)
    uploadUser = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    image = models.ImageField(upload_to='course/video/img', default='course/video/img/default.png')
    title = models.CharField(max_length=60)
    text = models.CharField(max_length=60)

    type = models.IntegerField(choices=typeChoices, default=0)

    # type in [0]
    BVID = models.TextField(max_length=20, null=True, blank=True)
    page = models.IntegerField(null=True, blank=True,default=1)

    # type in [1,3]
    url = models.TextField(max_length=20, null=True, blank=True)

    # type in [3]
    startTime = models.DateTimeField(blank=True, null=True)
    endTime = models.DateTimeField(blank=True, null=True)

    # type in [2]
    video = models.FileField(upload_to='course/video', null=True,blank=True)
    size = models.CharField(max_length=20, null=True, blank=True)
    duration = models.CharField(max_length=20, null=True, blank=True)


class Comment(models.Model):
    courseTo = models.ForeignKey(Course, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    addTime = models.DateTimeField(auto_now_add=True)
    text = models.TextField(max_length=500, blank=True)


class Issue(models.Model):
    statusChoices = [(0, '已开启'), (1, '已解决'), (2, '已关闭')]
    courseTo = models.ForeignKey(Course, on_delete=models.CASCADE)
    text = models.TextField(max_length=1000, blank=True, null=True)
    title = models.TextField(max_length=100, blank=True, null=True)
    status = models.IntegerField(choices=statusChoices, default=0)
    starterTo = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    startTime = models.DateTimeField(auto_now_add=True)
    lastReplyTime = models.DateTimeField(null=True, blank=True)


class Reply(models.Model):
    issueTo = models.ForeignKey(Issue, on_delete=models.CASCADE)
    text = models.TextField(max_length=1000)
    replyTime = models.DateTimeField(auto_now=True)
    userTo = models.ForeignKey(User, on_delete=models.CASCADE)


# class Question(models.Model):
#
# class Test(models.Model):
#
# class Homework(models.Model):

@receiver(models.signals.post_delete, sender=Video)
def auto_delete_file_on_delete(sender, instance, **kwargs):
    if instance.video:
        if os.path.isfile(instance.video.path):
            os.remove(instance.video.path)
    if instance.image:
        if os.path.isfile(instance.image.path):
            os.remove(instance.image.path)
